import {
  legacy_createStore, applyMiddleware
} from 'redux'
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import { CHILIDATA, ALLTYPELIST, ADD_LIST_DATA_TYPE, PUSHLISTDATA, ONTLIATDATA, STOPELIST, OPCLITRY } from '../types/index.d'

const initialState = {
  list: [],
  chliteh: [],
  listpush: [],  //添加到购物车
  addlist: [] 
}

const store = (state: ALLTYPELIST = initialState, { type, payload }: STOPELIST) => {
  switch (type) {

    case ONTLIATDATA:
      state.list = payload
      state.chliteh = payload[0].children
      return { ...state }

    case CHILIDATA:
      state.chliteh = state.list[payload].children
      return { ...state }

    case OPCLITRY:
      state.chliteh.filter(item => item.state1 === payload)
      console.log(state.chliteh.filter(item => item.state1 === payload), '1111');

      return { ...state }


    case PUSHLISTDATA:

      const pushlist = [...state.listpush]
      pushlist.push(payload)
      console.log(payload, '11122');
      state.listpush = pushlist
      return { ...state }

    //++
    case ADD_LIST_DATA_TYPE:
      const addlist = [...state.listpush]  //不能操作原数组要拷贝出来
      addlist.forEach((item) => {
        if (item.id == payload.id) {
          console.log(111);
          item.num++
        }
      })
      
      state.listpush=addlist
      console.log(payload, '11111******');
      return { ...state }




    default:
      return state
  }
}

const storelist = legacy_createStore(store, applyMiddleware(logger, thunk))

export default storelist